package com.myBlast.server;
import java.io.BufferedReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//import com.google.gwt.dev.util.collect.HashMap;

/**
 * This class provides methods to fetch data from Blast hit 
 * files. It has to be instantiated on a per file basis i.e.
 * for each Blast file there is one object. Currently it only
 * provides method to read a specific portion of the file.
 * @author Parit Bansal
 */
public class FetchHitData 
{
	private BufferedReader buff = null;
	private boolean flag = false;
	
	/**
	 * @param file The Blast hit file's complete path
	 * @throws FileNotFoundException
	 */
	public FetchHitData(String file) throws FileNotFoundException
	{
		buff = new BufferedReader(new FileReader(file));
	}
	
	/**
	 * This method mimics an iterator over the file.
	 * It reads through the Blast file till it reaches the 
	 * section that has summary of Blast results and then with
	 * each call reads and returns online result at a time.
	 * The returned array is something like this: 
	 * [gi|1236702|gb|AAA92974.1|, avirulence protein AvrXa10 [Xanthomona..., 18.9, , 0.43]
	 * There is a no space character that is read extra.
	 * @return an array containing different fields in the 
	 * summaried hit in Blast file
	 * @throws IOException
	 */
	public String[] getNext() throws IOException
	{
		String readLine = buff.readLine();
		while(readLine != null)
		{
			if (readLine.contains("Bits"))
			{
				flag = true;
			}
			else if (readLine.startsWith(">"))
			{
				return null;
			}
			else if (flag && !readLine.trim().equals(""))
			{
				readLine = readLine.trim().replaceAll("(  )+", "  ");
				return readLine.trim().split("  ");
				
			}
			readLine = buff.readLine();
		}
		return null;
	}
} // FetchHitData
